import { useEffect, useState } from "react"
import { message, Modal } from 'antd'

const useCURD = ({
  apis,
  defaultQuery = {},
  defaultSubmitValues = {},
  moduleName = '',
  initQuery = true,
  getResData = (res) => {
    return res.data
  },
  // 增删改成功之后的回调
  // 比如创建User成功之后刷新store里的user
  changeListSuccessCallBack
}) => {


  const [listData, setListData] = useState([])
  const [query, setQuery] = useState({
    ...defaultQuery
  })
  const [loading, setLoading] = useState(false)
  const [selectedRowKeys, setSelectedRowKeys] = useState([])

  const [formVisible, setFormVisible] = useState(false)
  const [formTitle, setFormTitle] = useState('')
  const [formData, setFormData] = useState({})


  const rowSelection = {
    selectedRowKeys,
    onChange: (selectedRowKeys) => {
      setSelectedRowKeys(selectedRowKeys)
    },
  }

  const changeQuery = (key, value) => {
    setQuery({
      ...query,
      [key]: value
    })
  }

  const changeQueryValues = (obj) => {
    setQuery({
      ...query,
      ...obj
    })
  }

  useEffect(() => {
    if (initQuery) {
      doQueryList()
    }
  }, [query])

  const doQueryList = async () => {
    if (!loading) {
      setLoading(true)
      try {
        const res = await apis.queryList({
          ...query
        })
        setListData(getResData(res))
        setLoading(false)
      } catch (error) {
        setLoading(false)
      }
    }
  }

  const handleSubmit = async (values, callback) => {
    try {
      if (formData.id) {
        await doModify(formData.id, {
          ...values,
          ...defaultSubmitValues
        })
      } else {
        await doCreate({
          ...values,
          ...defaultSubmitValues
        })
      }
    } catch (error) {
      callback && callback()
    }
  }

  const doCreate = async (values) => {
    await apis.createOne(values)
    message.success(`${moduleName}创建成功`)
    setFormVisible(false)
    setFormTitle('')
    setFormData({})
    doQueryList()
    changeListSuccessCallBack && changeListSuccessCallBack()
  }

  const doModify = async (id, values) => {
    await apis.modifyOne(id, values)
    message.success(`${moduleName}修改成功`)

    setFormVisible(false)
    setFormTitle('')
    setFormData({})
    doQueryList()
    changeListSuccessCallBack && changeListSuccessCallBack()
  }


  const handleDeleteBatch = (ids, callback) => {
    Modal.confirm({
      title: '确定要删除选中项？',
      onOk: async () => {
        for (let id of ids) {
          await apis.deleteOne({ id })
        }
        doQueryList()
        message.success(`${moduleName}删除成功`)
        callback && callback()
        changeListSuccessCallBack && changeListSuccessCallBack()
      }
    })
  }

  const handleDeleteOne = (data) => {
    Modal.confirm({
      title: '确定要删除此项？',
      onOk: async () => {
        await apis.deleteOne(data)
        message.success(`${moduleName}删除成功`)
        doQueryList()
        changeListSuccessCallBack && changeListSuccessCallBack()
      }
    })
  }

  const handleCreate = () => {
    setFormVisible(true)
    setFormTitle(`创建${moduleName}`)
    setFormData({})
  }

  const handleModify = (data) => {
    setFormVisible(true)
    setFormTitle(`编辑${moduleName}`)
    setFormData(data)
  }

  const handleCancelForm = () => {
    setFormVisible(false)
    setFormTitle('')
  }

  // useEffect(() => {
  //   doQueryList()
  // }, [])

  return {
    listData,
    loading,
    rowSelection,
    selectedRowKeys,
    setSelectedRowKeys,
    query,
    changeQuery,
    changeQueryValues,
    doQueryList,
    handleCreate,
    handleModify,
    handleCancelForm,
    handleSubmit,
    formVisible,
    formTitle,
    formData,
    handleDeleteBatch,
    handleDeleteOne
  }
}

export default useCURD